/*
 * @Author: dargon 17864730373@163.com
 * @Date: 2025-09-12 14:39:38
 * @LastEditors: dargon 17864730373@163.com
 * @LastEditTime: 2025-09-16 09:42:46
 * @FilePath: \vue-sound\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/LoginView.vue'),
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('@/views/DashboardView.vue'),
    meta: {
      title: '仪表盘',
      icon: 'House',
      requiresAuth: true
    }
  },
  {
    path: '/speech',
    name: 'SpeechToText',
    component: () => import('@/views/SpeechToTextView.vue'),
    meta: {
      title: '语音转文字',
      icon: 'Microphone',
      requiresAuth: true
    }
  },
  // 设置
  {
    path: '/settings',
    name: 'Settings',
    component: () => import('@/views/SettingsView.vue'),
    meta: {
      title: '设置',
      icon: 'Setting',
      requiresAuth: true
    }
  },
  {
    path: '/pdf',
    name: 'PdfViewer',
    component: () => import('@/views/PdfViewerView.vue'),
    meta: {
      title: 'PDF预览',
      icon: 'Document',
      requiresAuth: true
    }
  },
  {
    path: '/table',
    name: 'DataTable',
    component: () => import('@/components/DataTableComponent.vue'),
    meta: {
      title: '数据表格',
      icon: 'Grid',
      requiresAuth: true
    }
  },
  {
    path: '/users',
    name: 'UserManagement',
    component: () => import('@/views/UserManagementView.vue'),
    meta: {
      title: '用户管理',
      icon: 'User',
      requiresAuth: true
    }
  },
  {
    path: '/shandong-map',
    name: 'ShandongMap',
    component: () => import('@/views/AboutView.vue'),
    meta: {
      title: '山东省地图',
      icon: 'MapLocation',
      requiresAuth: true
    }
  },
  {
    path: '/invoice',
    name: 'ElectronicInvoice',
    component: () => import('@/views/InvoiceView.vue'),
    meta: {
      title: '电子发票',
      icon: 'Money',
      requiresAuth: true
    }
  },
  {
    path: '/invoice/list',
    name: 'InvoiceList',
    component: () => import('@/views/InvoiceListView.vue'),
    meta: {
      title: '发票列表',
      requiresAuth: true
    }
  },
  {
    path: '/invoice/form',
    name: 'InvoiceForm',
    component: () => import('@/views/InvoiceFormView.vue'),
    meta: {
      title: '发票填写',
      requiresAuth: true
    }
  },
  {
    path: '/invoice/confirm',
    name: 'InvoiceConfirm',
    component: () => import('@/views/InvoiceConfirmView.vue'),
    meta: {
      title: '确认开票',
      requiresAuth: true
    }
  },
  {
    path: '/invoice/result',
    name: 'InvoiceResult',
    component: () => import('@/views/InvoiceResultView.vue'),
    meta: {
      title: '开票结果',
      requiresAuth: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, _from, next) => {
  const authStore = useAuthStore()
  
  // 初始化认证状态
  authStore.initAuth()
  
  // 检查是否需要认证
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  
  if (requiresAuth) {
    // 需要认证的路由
    if (authStore.isAuthenticated) {
      // 已登录，允许访问
      next()
    } else {
      // 未登录，重定向到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    // 不需要认证的路由（如登录页）
    if (to.path === '/login' && authStore.isAuthenticated) {
      // 已登录用户访问登录页，重定向到首页
      next('/dashboard')
    } else {
      // 允许访问
      next()
    }
  }
})

export default router
